from django.db import models
from assets.models import Unit, Device
from users.models import User, Visitor, Company


class Contract(models.Model):
    contract_id = models.BigAutoField(primary_key=True, db_comment='合同ID')
    contract_number = models.CharField(unique=True, max_length=50, db_comment='合同编号')
    contract_name = models.CharField(max_length=100, db_comment='合同名称')
    contract_type = models.CharField(max_length=50, db_comment='合同类型(房源租赁/车位租赁/物业服务/其他)')
    company = models.ForeignKey(Company, models.DO_NOTHING, blank=True, null=True, db_comment='乙方企业ID')
    user = models.ForeignKey('users.User', models.DO_NOTHING, blank=True, null=True, db_comment='乙方用户ID')
    party_b = models.CharField(max_length=100, db_comment='甲方主体')
    unit = models.ForeignKey('assets.Unit', models.DO_NOTHING, blank=True, null=True, db_comment='关联单元ID')
    vehicle = models.ForeignKey('assets.Vehicle', models.DO_NOTHING, blank=True, null=True, db_comment='关联车辆ID')
    contract_amount = models.DecimalField(max_digits=12, decimal_places=2, db_comment='合同金额')
    payment_cycle = models.CharField(max_length=20, blank=True, null=True, db_comment='支付周期')
    start_date = models.DateField(db_comment='开始日期')
    end_date = models.DateField(db_comment='结束日期')
    sign_date = models.DateField(db_comment='签约日期', blank=True, null=True)
    status = models.CharField(max_length=20, db_comment='合同状态(草稿/待审核/生效中/已到期/已终止/作废)')
    attachment_urls = models.TextField(blank=True, null=True, db_comment='附件URL列表(JSON)')
    remarks = models.TextField(blank=True, null=True, db_comment='备注')
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'contract'
        db_table_comment = '合同表'

class ParkingSpaceLeasingService(models.Model):
    application_id = models.BigAutoField(primary_key=True, db_comment='申请ID')
    applicant_user = models.ForeignKey('users.User', models.DO_NOTHING, db_comment='申请用户ID')
    unit = models.ForeignKey('assets.Unit', models.DO_NOTHING, db_comment='意向车位单元ID')
    vehicle = models.ForeignKey('assets.Vehicle', models.DO_NOTHING, db_comment='关联车辆ID')
    expected_start_date = models.DateField(db_comment='期望租赁开始日期')
    expected_duration = models.IntegerField(db_comment='期望租赁时长(月)')
    application_time = models.DateTimeField(db_comment='申请时间')
    status = models.CharField(max_length=20, db_comment='状态(待审核/审核中/审核通过/审核驳回/已取消)')
    approval_opinion = models.TextField(blank=True, null=True, db_comment='审批意见')
    contract = models.ForeignKey(Contract, models.DO_NOTHING, blank=True, null=True, db_comment='关联合同ID')
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'parking_space_leasing_service'
        db_table_comment = '车位租赁服务表'


class RepairService(models.Model):
    repair_id = models.BigAutoField(primary_key=True, db_comment='报修ID')
    applicant_user = models.ForeignKey('users.User', models.DO_NOTHING, db_comment='申请用户ID')
    unit = models.ForeignKey('assets.Unit', models.DO_NOTHING, blank=True, null=True, db_comment='关联单元ID')
    device = models.ForeignKey(Device, models.DO_NOTHING, blank=True, null=True, db_comment='关联设备ID')
    fault_description = models.TextField(db_comment='故障描述')
    media_urls = models.TextField(blank=True, null=True, db_comment='图片/视频证据URL列表(JSON)')
    expected_time = models.DateTimeField(blank=True, null=True, db_comment='期望处理时间')
    application_time = models.DateTimeField(db_comment='申请时间')
    status = models.CharField(max_length=20, db_comment='状态(待受理/已受理/处理中/已完成/已评价/已取消)')
    handler_user = models.ForeignKey('users.User', models.DO_NOTHING, related_name='repairservice_handler_user_set', blank=True, null=True, db_comment='指派处理人用户ID')
    completion_time = models.DateTimeField(blank=True, null=True, db_comment='完成时间')
    repair_result = models.TextField(blank=True, null=True, db_comment='维修结果描述')
    repair_cost = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='维修费用')
    bill_id = models.BigIntegerField(blank=True, null=True, db_comment='关联账单ID')
    rating = models.IntegerField(blank=True, null=True, db_comment='用户评价星级(1-5)')
    review_content = models.TextField(blank=True, null=True, db_comment='用户评价内容')
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'repair_service'
        db_table_comment = '物业报修服务表'


class DeviceMaintenance(models.Model):
    maintenance_id = models.BigAutoField(primary_key=True, db_comment='维保记录ID')
    device = models.ForeignKey('assets.Device', models.DO_NOTHING, db_comment='关联设备ID')
    maintenance_type = models.CharField(max_length=10, db_comment='维保类型(维修/巡检)')
    content = models.TextField(db_comment='维保内容描述')
    responsible_person = models.CharField(max_length=100, blank=True, null=True, db_comment='负责人')
    maintenance_date = models.DateField(db_comment='维保日期')
    cost = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='费用')
    repair_service = models.OneToOneField('RepairService', models.DO_NOTHING, blank=True, null=True, db_comment='关联的报修服务ID')
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'device_maintenance_record'
        db_table_comment = '设备维保记录表(包含维修和巡检)'
        ordering = ['-maintenance_date']


class TemporaryParkingService(models.Model):
    parking_record_id = models.BigAutoField(primary_key=True, db_comment='停车记录ID')
    vehicle = models.ForeignKey('assets.Vehicle', models.DO_NOTHING, blank=True, null=True, db_comment='车辆ID')
    plate_number = models.CharField(max_length=20, db_comment='车牌号码')
    entry_time = models.DateTimeField(db_comment='入场时间')
    exit_time = models.DateTimeField(blank=True, null=True, db_comment='出场时间')
    parking_duration = models.IntegerField(blank=True, null=True, db_comment='停车时长(分钟)')
    fee_rate = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='费用标准(元/小时)')
    payable_amount = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='应付金额')
    bill_id = models.BigIntegerField(blank=True, null=True, db_comment='关联账单ID')
    entry_channel = models.CharField(max_length=50, blank=True, null=True, db_comment='入口通道')
    exit_channel = models.CharField(max_length=50, blank=True, null=True, db_comment='出口通道')
    user = models.ForeignKey('users.User', models.DO_NOTHING, blank=True, null=True, db_comment='关联用户ID')
    visitor = models.ForeignKey('users.Visitor', models.DO_NOTHING, blank=True, null=True, db_comment='关联访客ID')
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'temporary_parking_service'
        db_table_comment = '临时停车服务表'

class VisitorAppointmentService(models.Model):
    appointment_id = models.BigAutoField(primary_key=True, db_comment='预约ID')
    inviter_user = models.ForeignKey('users.User', models.DO_NOTHING, db_comment='邀请人用户ID')
    visited_company = models.ForeignKey('users.Company', models.DO_NOTHING, blank=True, null=True, db_comment='被访企业ID')
    visitor = models.ForeignKey('users.Visitor', models.DO_NOTHING, db_comment='关联访客ID')
    expected_visit_time = models.DateTimeField(db_comment='预计到访日期时间')
    visit_purpose = models.TextField(db_comment='访问事由')
    status = models.CharField(max_length=20, db_comment='状态(待审批/已批准/已拒绝/已入园/已离园/已取消)')
    accompanier_info = models.TextField(blank=True, null=True, db_comment='陪同人信息')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        managed = False
        db_table = 'visitor_appointment_service'
        db_table_comment = '访客预约服务表'


class WaterDeliveryService(models.Model):
    service_id = models.BigAutoField(primary_key=True, db_comment='送水服务ID')
    applicant_user = models.ForeignKey('users.User', models.DO_NOTHING, db_comment='申请用户ID')
    unit = models.ForeignKey('assets.Unit', models.DO_NOTHING, blank=True, null=True, db_comment='关联单元ID')
    product_name = models.CharField(max_length=100, db_comment='水产品名称/规格')
    quantity = models.IntegerField(db_comment='数量')
    unit_price = models.DecimalField(max_digits=10, decimal_places=2, db_comment='单价')
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, db_comment='总金额')
    expected_delivery_time = models.DateTimeField(db_comment='期望送达日期时间')
    delivery_address = models.TextField(db_comment='详细配送地址/楼层房号')
    contact_name = models.CharField(max_length=50, db_comment='联系人姓名')
    contact_phone = models.CharField(max_length=20, db_comment='联系电话')
    application_time = models.DateTimeField(db_comment='申请时间')
    status = models.CharField(max_length=20, db_comment='状态(待受理/已确认备货中/配送中/已送达/已评价/已取消)')
    deliverer_user = models.ForeignKey('users.User', models.DO_NOTHING, related_name='waterdeliveryservice_deliverer_user_set', blank=True, null=True, db_comment='指派配送员用户ID')
    actual_delivery_time = models.DateTimeField(blank=True, null=True, db_comment='实际送达时间')
    bill_id = models.BigIntegerField(blank=True, null=True, db_comment='关联账单ID')
    rating = models.IntegerField(blank=True, null=True, db_comment='用户评价星级(1-5)')
    review_content = models.TextField(blank=True, null=True, db_comment='用户评价内容')
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'water_delivery_service'
        db_table_comment = '送水服务表'


class DecorationApplication(models.Model):
    application_id = models.BigAutoField(primary_key=True, db_comment='申请ID')
    applicant_user = models.ForeignKey('users.User', models.DO_NOTHING, db_comment='申请用户ID')
    unit = models.ForeignKey('assets.Unit', models.DO_NOTHING, db_comment='关联单元ID')
    application_date = models.DateField(db_comment='申请日期')
    design_plan_url = models.TextField(blank=True, null=True, db_comment='装修方案URL')
    construction_company = models.CharField(max_length=100, db_comment='施工单位')
    construction_staff_info = models.TextField(blank=True, null=True, db_comment='施工人员信息(JSON)')
    estimated_duration = models.IntegerField(db_comment='预计工期(天)')
    status = models.CharField(max_length=20, db_comment='状态(待审核/审核通过/审核驳回/施工中/已完工/已验收)')
    approval_opinion = models.TextField(blank=True, null=True, db_comment='审批意见')
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'decoration_application'
        db_table_comment = '装修备案表'


class LeasingApplicationService(models.Model):
    application_id = models.BigAutoField(primary_key=True, db_comment='申请ID')
    applicant_user = models.ForeignKey('users.User', models.DO_NOTHING, db_comment='申请用户ID')
    company = models.ForeignKey(Company, models.DO_NOTHING, blank=True, null=True, db_comment='申请企业ID')
    unit = models.ForeignKey('assets.Unit', models.DO_NOTHING, db_comment='意向单元ID')
    expected_start_date = models.DateField(db_comment='期望租赁开始日期')
    expected_duration = models.IntegerField(db_comment='期望租赁时长(月)')
    application_time = models.DateTimeField(db_comment='申请时间')
    status = models.CharField(max_length=20, db_comment='状态(待审核/审核中/审核通过/审核驳回/已取消)')
    approval_opinion = models.TextField(blank=True, null=True, db_comment='审批意见')
    contract = models.ForeignKey(Contract, models.DO_NOTHING, blank=True, null=True, db_comment='关联合同ID')
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'leasing_application_service'
        db_table_comment = '租赁申请服务表'


class CleaningBookingService(models.Model):
    booking_id = models.BigAutoField(primary_key=True, db_comment='预约ID')
    applicant_user = models.ForeignKey('users.User', models.DO_NOTHING, db_comment='申请用户ID')
    unit = models.ForeignKey('assets.Unit', models.DO_NOTHING, db_comment='关联单元ID')
    expected_service_date = models.DateField(db_comment='期望服务日期')
    expected_time_slot = models.CharField(max_length=50, db_comment='期望服务时间段')
    special_requirements = models.TextField(blank=True, null=True, db_comment='特殊要求')
    application_time = models.DateTimeField(db_comment='申请时间')
    status = models.CharField(max_length=20, db_comment='状态(待受理/已受理/服务中/已完成/已评价/已取消)')
    handler_user = models.ForeignKey('users.User', models.DO_NOTHING, related_name='cleaningbookingservice_handler_user_set', blank=True, null=True, db_comment='指派处理人用户ID')
    completion_time = models.DateTimeField(blank=True, null=True, db_comment='完成时间')
    service_fee = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, db_comment='服务费用')
    bill_id = models.BigIntegerField(blank=True, null=True, db_comment='关联账单ID')
    rating = models.IntegerField(blank=True, null=True, db_comment='用户评价星级(1-5)')
    review_content = models.TextField(blank=True, null=True, db_comment='用户评价内容')
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'cleaning_booking_service'
        db_table_comment = '保洁预约服务表'
